package com.atguigu.mybatis.test;

import com.atguigu.mybatis.entity.Customer;
import com.atguigu.mybatis.entity.Order;
import com.atguigu.mybatis.mapper.CustomerMapper;
import com.atguigu.mybatis.mapper.OrderMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;
import java.util.concurrent.TimeUnit;

@SpringBootTest
public class DemoTest02 {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Test
    public void test01() {
        // 目标：根据 id 查询一个 Order 对象，同时要求在 Order 对象中把关联的 Customer 对象也查出来
        int orderId = 2;

        Order order = orderMapper.selectOrderWithCustomer(orderId);

        String orderName = order.getOrderName();
        System.out.println("orderName = " + orderName);

        Customer customer = order.getCustomer();
        Integer customerId = customer.getCustomerId();
        System.out.println("customerId = " + customerId);

        String customerName = customer.getCustomerName();
        System.out.println("customerName = " + customerName);
    }

    @Test
    public void test02() {
        // 目标：查询一个 Customer 对象，但是要求把 Customer 关联的 List<Order> 也查询出来
        int customerId = 1;

        Customer customer = customerMapper.selectCustomerWithOrderList(customerId);

        String customerName = customer.getCustomerName();
        System.out.println("customerName = " + customerName);

        // 立即加载：在查询 Customer 对象时就把级联的 Order List 查询出来，如果业务场景下 90% 的情况 Customer 不需要 Order List 那就会造成内存的浪费
        // 延迟加载：在查询 Customer 对象时先不查询 Order List，而是在用到 Order List 时才查询
        List<Order> orderList = customer.getOrderList();
        for (Order order : orderList) {
            Integer orderId = order.getOrderId();
            System.out.println("orderId = " + orderId);

            String orderName = order.getOrderName();
            System.out.println("orderName = " + orderName);
        }
    }

    @Test
    public void test03() throws InterruptedException {
        // 目标：查询 Customer 对象，对于关联的 OrderList 集合数据延迟加载
        // 实现步骤 1：先实现分步查询，也就是说，把一条 SQL 语句拆分为两条
        // 实现步骤 2：通过全局配置打开延迟加载功能
        int customerId = 1;

        Customer customer = customerMapper.selectCustomerLazyOrderList(customerId);

        String customerName = customer.getCustomerName();
        System.out.println("customerName = " + customerName);

        System.out.println("---------------------------------------------");
        // TimeUnit.SECONDS.sleep(3);

        // 立即加载：在查询 Customer 对象时就把级联的 Order List 查询出来，如果业务场景下 90% 的情况 Customer 不需要 Order List 那就会造成内存的浪费
        // 延迟加载：在查询 Customer 对象时先不查询 Order List，而是在用到 Order List 时才查询
        List<Order> orderList = customer.getOrderList();
        for (Order order : orderList) {
            Integer orderId = order.getOrderId();
            System.out.println("orderId = " + orderId);

            String orderName = order.getOrderName();
            System.out.println("orderName = " + orderName);
        }
    }
}
